﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace AutoParkingManagement
{
    public partial class ChangePassword : Form
    {
        #region Private members
        private string _username;
        private string _password;
        private bool IsValidPassword()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT Count(UserName) ");
            sb.Append("FROM Employee ");
            sb.Append("WHERE [Active] =1 AND [UserName]=@Username AND [Password]=@Password");
            string strCommandText = sb.ToString();
            SqlParameter[] parms = new SqlParameter[2];
            parms[0] = new SqlParameter();
            parms[0].ParameterName = "@UserName";
            parms[0].SqlDbType = SqlDbType.NVarChar;
            parms[0].Size = 50;
            parms[0].Value = _username;

            parms[1] = new SqlParameter();
            parms[1].ParameterName = "@Password";
            parms[1].SqlDbType = SqlDbType.NVarChar;
            parms[1].Size = 255;
            parms[1].Value = Common.Md5Encrypt(txtOldPassword.Text.Trim());

            object objPassword = SqlHelper.ExecuteScalar(CommandType.Text, strCommandText, parms);

            return ((int)objPassword > 0);            
        }

        private void UpdatePassword()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("UPDATE Employee ");
            sb.Append("SET [Password]= @NewPassword ");
            sb.Append("WHERE [UserName]=@Username AND [Password]=@OldPassword");
            string strCommandText = sb.ToString();
            SqlParameter[] parms = new SqlParameter[3];
            parms[0] = new SqlParameter();
            parms[0].ParameterName = "@UserName";
            parms[0].SqlDbType = SqlDbType.NVarChar;
            parms[0].Size = 50;
            parms[0].Value = _username;

            parms[1] = new SqlParameter();
            parms[1].ParameterName = "@OldPassword";
            parms[1].SqlDbType = SqlDbType.NVarChar;
            parms[1].Size = 255;
            parms[1].Value = Common.Md5Encrypt(txtOldPassword.Text.Trim()); ;

            parms[2] = new SqlParameter();
            parms[2].ParameterName = "@NewPassword";
            parms[2].SqlDbType = SqlDbType.NVarChar;
            parms[2].Size = 255;
            parms[2].Value = Common.Md5Encrypt(txtNewPassword1.Text.Trim());

            SqlHelper.ExecuteNonQuery(CommandType.Text, strCommandText, parms);
        }
        #endregion

        #region Properties
        #endregion

        #region Public members
        public delegate void PasswordChangedHandler(bool isChanged);
        public event PasswordChangedHandler PasswordChanged;        
        #endregion

        #region Events

        private void ChangePassword_Load(object sender, EventArgs e)
        {

        }

        #endregion

        

        public ChangePassword(string username, string password)
        {
            InitializeComponent();
            _username = username;
            _password = password;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            PasswordChanged(false);            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            txtOldPassword.Text = txtOldPassword.Text.Trim();
            txtNewPassword1.Text = txtNewPassword1.Text.Trim();
            txtNewPassword2.Text = txtNewPassword2.Text.Trim();
            if (txtNewPassword1.Text != txtNewPassword2.Text)
            {
                MessageBox.Show("Mật khẩu xác nhận sai.");
                return;
            }

            if (!IsValidPassword())
            {
                MessageBox.Show("Mật khẩu cũ sai.");
                return;
            }

            UpdatePassword();

            this.Hide();
            MessageBox.Show("Mật khẩu đã thay đổi \n Vui lòng đăng nhập lại");

            this.Close();
            PasswordChanged(true);            


        }

        private void txtOldPassword_TextChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = !(String.IsNullOrEmpty(txtOldPassword.Text.Trim()) ||
                            String.IsNullOrEmpty(txtNewPassword1.Text.Trim()) ||
                            String.IsNullOrEmpty(txtNewPassword2.Text.Trim()));
        }

        private void txtNewPassword1_ValueChanged(object sender, EventArgs e)
        {
            txtOldPassword_TextChanged(null, null);
        }

        private void txtNewPassword2_ValueChanged(object sender, EventArgs e)
        {
            txtOldPassword_TextChanged(null, null);
        }

        
    }
}
